#!/usr/bin/perl

use strict;
use warnings;

select STDERR; $| = 1;
select STDOUT; $| = 1;
#use Template::Toolkit;
use Data::Dumper;

# specify input filename, or file handle, text reference, etc.
my $input = $ARGV[0] || die "need filename\n";

print <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<!--
	* 1st January 2010
	* List of Boots shop locations
	* https://code.google.com/p/scrapeshops/
-->
<kml xmlns="http://www.opengis.net/kml/2.2">
EOF

open(IN,'<',$input) || die $!;
my $header = <IN>; # throw away

while (my $line = <IN>) { 
	chomp($line);

	# Boots store name	Latitude	Longitude	Address1	Address2	Address3	Postcode	Phone number
	# London Bridge St	51.501401	-0.124976	11 Bridge Street	null	null	SW1A 2JR	0207 9304571

	#die "line=$line";
	my @a = split(/\t/,$line);
	#die "a=".Dumper(\@a);
	my ($name, $lat, $long) = ($a[0],$a[1],$a[2]);
	my ($phone) = ($a[7]);
	my (@address) = ($a[3],$a[4],$a[5],$a[6]);
	my $description = "$name ($lat,$long)\n";
	my $formatted_address = "\n";
	foreach my $add (@address) {
		$formatted_address .= "$add\n";
	}
	$description .= "$formatted_address\n";
	$description .= "Tel: $phone\n";
	#die "desc=$description";

	print <<EOF;
	  <Placemark>
	    <name>$name</name>
	    <description>$description</description>
	    <Point>
	      <coordinates>$long,$lat,0</coordinates>
	    </Point>
	  </Placemark>
EOF

	#die "argh";
}

print "</kml>\n";

close(IN);

# process input template, substituting variables
#$template->process($input, $vars)
#	|| die $template->error();

